package Controler;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Model.CVDAO;

public class updateCVNow extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public updateCVNow() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String UUser = request.getParameter("UUser");
		String Sex = request.getParameter("USex");
		short USex = 0;
		String UEmail = request.getParameter("UEmail");
		int UTel = Integer.parseInt(request.getParameter("UTel"));
		String UAddress = request.getParameter("UAddress");
		int UPostCode = Integer.parseInt(request.getParameter("UPostCode"));
		String Education = request.getParameter("Education");
		String Speciality =request.getParameter("Speciality");
		String JType = request.getParameter("JType");
		String Job = request.getParameter("Job");
		String City = request.getParameter("City");
		int CVNO = Integer.parseInt(request.getParameter("City"));
		int Expectation = Integer.parseInt(request.getParameter("Expectation"))*1000;
		String SelfIntroduction = request.getParameter("SelfIntroduction");
		CVDAO cvdao = new CVDAO();
		if(Sex.equals("男"))
			USex = 1;
		else
			USex = 0;
		try {
			if(cvdao.UpdateCV(CVNO,UUser, USex, UEmail, UTel, UAddress, UPostCode, Education, Speciality, JType, Job, City, Expectation, SelfIntroduction))
			{
				out.print("<script>alert('update CV successfully');window.location.href='person.jsp';</script>");
			}
				
			else {
				out.print("<script>alert('update new CV failed');window.location.href='person.jsp';</script>");
			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
